import {
  getHomeData
} from '../../api/home'
import {
  HOME_LIST,
  HOME_SALE_LIST,
  HOME_CUSTOMER_LIST,
  HOME_SALE_RANK_LIST,
  HOME_PRODUCT_RANK_LIST
} from '../motations-type'

export default {
  state: {
    homeList: window.sessionStorage.getItem('home_list')
      ? JSON.parse(window.sessionStorage.getItem('home_list'))
      : [],
    homeSaleList: window.sessionStorage.getItem('home_sale_list')
      ? JSON.parse(window.sessionStorage.getItem('home_sale_list'))
      : {},
    homeCustomerList: window.sessionStorage.getItem('home_customer_list')
      ? JSON.parse(window.sessionStorage.getItem('home_customer_list'))
      : {},
    homeSaleRankList: window.sessionStorage.getItem('home_sale_rank_list')
      ? JSON.parse(window.sessionStorage.getItem('home_sale_rank_list'))
      : {},
    homeProductRankList: window.sessionStorage.getItem('home_product_rank_list')
      ? JSON.parse(window.sessionStorage.getItem('home_product_rank_list'))
      : {}
  },
  getters: {
    homeList: state => state.homeList,
    homeSaleList: state => state.homeSaleList,
    homeCustomerList: state => state.homeCustomerList,
    homeSaleRankList: state => state.homeSaleRankList,
    homeProductRankList: state => state.homeProductRankList
  },
  mutations: {
    [HOME_LIST] (state, data) {
      state.homeList = data
      window.sessionStorage.setItem('home_list', JSON.stringify(data))
    },
    [HOME_SALE_LIST] (state, data) {
      state.homeSaleList = data
      window.sessionStorage.setItem('home_sale_list', JSON.stringify(data))
    },
    [HOME_CUSTOMER_LIST] (state, data) {
      state.homeCustomerList = data
      window.sessionStorage.setItem('home_customer_list', JSON.stringify(data))
    },
    [HOME_SALE_RANK_LIST] (state, data) {
      state.homeSaleRankList = data
      window.sessionStorage.setItem('home_sale_rank_list', JSON.stringify(data))
    },
    [HOME_PRODUCT_RANK_LIST] (state, data) {
      state.homeProductRankList = data
      window.sessionStorage.setItem('home_product_rank_list', JSON.stringify(data))
    }
  },
  actions: {
    async getHomeList ({ commit, rootState }, playload) {
      window.sessionStorage.removeItem('home_list')
      window.sessionStorage.removeItem('home_sale_list')
      window.sessionStorage.removeItem('home_customer_list')
      window.sessionStorage.removeItem('home_sale_rank_list')
      window.sessionStorage.removeItem('home_product_rank_list')
      let data = {}
      let newData = []
      let homeSaleList = {}
      let countList = []
      let dateList = []
      let customerCount = []
      let customerDate = []
      let homeCustomerList = {}
      let saleRankCount = []
      let saleRankDate = []
      let saleRankList = {}
      let productRankCount = []
      let productRankDate = []
      let productRankList = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getHomeData(data)
      resp.data.data.sale.table.map((item) => {
        countList.push(item.count)
        dateList.push(item.day)
      })
      resp.data.data.customer.table.map((item) => {
        customerCount.push(item.count)
        customerDate.push(item.day)
      })
      resp.data.data.salerank.map((item) => {
        saleRankCount.push(item.mprice)
        saleRankDate.push(item.mname)
      })
      resp.data.data.productrank.map((item) => {
        productRankCount.push(item.mprice)
        productRankDate.push(item.name)
      })
      homeSaleList.date = dateList
      homeSaleList.count = countList
      homeCustomerList.date = customerDate
      homeCustomerList.count = customerCount
      saleRankList.date = saleRankDate
      saleRankList.count = saleRankCount
      productRankList.date = productRankDate
      productRankList.count = productRankCount
      commit(HOME_SALE_LIST, homeSaleList)
      commit(HOME_CUSTOMER_LIST, homeCustomerList)
      commit(HOME_SALE_RANK_LIST, saleRankList)
      commit(HOME_PRODUCT_RANK_LIST, productRankList)
      commit(HOME_LIST, resp.data.data)
    }
  }
}
